#--*--coding=utf-8--*--
#MaterialCodeGeneratorMain1.py
#

import sys
from PySide6.QtWidgets import (QApplication, QMainWindow, QGridLayout, QVBoxLayout, QHBoxLayout, QComboBox,
                               QMenu, QLabel, QFileDialog, QMessageBox, QDialog, QTextEdit, QPushButton,QWidget,QGroupBox)
from PySide6.QtGui import QIcon, QPixmap, QAction
from PySide6.QtCore import Qt, QUrl, QSize
from qt_material import apply_stylesheet, list_themes

class AboutDialog(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("关于")
        self.ThemeDialog.apply_theme()
        layout = QVBoxLayout()
        
        # 显示图片
        image_label = QLabel()
        pixmap = QPixmap("./icon/icon.png")  # 替换为你的图片路径
        image_label.setPixmap(pixmap.scaled(200, 200, Qt.KeepAspectRatio, Qt.SmoothTransformation))
        layout.addWidget(image_label, alignment=Qt.AlignCenter)
        
        # 显示说明文本
        text_edit = QTextEdit()
        text_edit.setPlainText("这是一个示例程序。\n更多信息请访问：www.baidu.com")
        text_edit.setReadOnly(True)
        layout.addWidget(text_edit)

        self.setLayout(layout)

class ThemeDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("选择主题")
        layout = QVBoxLayout()

        self.theme_combobox = QComboBox()
        self.theme_combobox.addItems(list_themes())#(["light_blue.xml", "dark_amber.xml", "light_cyan.xml", "dark_teal.xml"])
        layout.addWidget(self.theme_combobox)

        apply_button = QPushButton("应用")
        apply_button.clicked.connect(self.apply_theme)
        layout.addWidget(apply_button)

        self.setLayout(layout)

    def apply_theme(self):
        theme_name = self.theme_combobox.currentText()
        apply_stylesheet(self.parent(), theme=theme_name)
        self.close()

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("主窗口")
        self.setGeometry(100, 100, 800, 500)
        
        # 创建菜单栏
        menubar = self.menuBar()
        
        file_menu = QMenu("文件", self)
        exit_action = QAction("退出", self)
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)
        menubar.addMenu(file_menu)
        
        help_menu = QMenu("帮助", self)
        about_action = QAction("关于", self)
        about_action.triggered.connect(self.show_about_dialog)
        help_menu.addAction(about_action)
        
        theme_action = QAction("主题", self)
        theme_action.triggered.connect(self.show_theme_dialog)
        help_menu.addAction(theme_action)
        
        menubar.addMenu(help_menu)
        
        # 创建中心部件
        central_widget = QWidget(self)
        self.setCentralWidget(central_widget)
        
        # 创建网格布局
        grid_layout = QGridLayout(central_widget)
        
        # AD组合框
        ad_groupbo_box = QGroupBox("AD")#QComboBox()
        ad_layout = QHBoxLayout()
#         for i in range(4):
#             #icon_label = QLabel(self)
#             
#             pixmap = QPixmap(f"icon{i+1}.png").scaled(30,30, Qt.KeepAspectRatio,Qt.SmoothTransformation)
#             icon_label.setPixmap(pixmap)
#             ad_layout.addWidget(icon_label)
#         ad_groupbo_box.setLayout(ad_layout)
#         grid_layout.addWidget(ad_groupbo_box)
        
        for i in range(4):
            icon_btn = QPushButton(self)
            icon_btn.setIcon(QIcon(f"./icon/icon256x256.png"))#.scaled(30,30, Qt.KeepAspectRatio,Qt.SmoothTransformation))
            icon_btn.setIconSize(QSize(30,30))
            ad_layout.addWidget(icon_btn)
        ad_groupbo_box.setLayout(ad_layout)
        grid_layout.addWidget(ad_groupbo_box) 
        
            
        # 功能组合框
        function_combo_box = QGroupBox("制作数据库")
        function_layout = QGridLayout()
        
        # 制作数据库图标和标签
        database_icon = QLabel()
        database_icon.setPixmap(QPixmap("./icon/icon256x256.png").scaled(30, 30, Qt.KeepAspectRatio, Qt.SmoothTransformation))
        function_layout.addWidget(database_icon, 0, 0, alignment=Qt.AlignCenter)
        database_label = QLabel("制作数据库")
        function_layout.addWidget(database_label, 1, 0, alignment=Qt.AlignCenter)
        database_icon.mousePressEvent = lambda e: self.launch_sub_program("generate_database.py")
        
        # 制作编码图标和标签
        code_icon = QLabel()
        code_icon.setPixmap(QPixmap("./icon/icon256x256.png").scaled(30, 30, Qt.KeepAspectRatio, Qt.SmoothTransformation))
        function_layout.addWidget(code_icon, 0, 1, alignment=Qt.AlignCenter)
        code_label = QLabel("制作编码")
        function_layout.addWidget(code_label, 1, 1, alignment=Qt.AlignCenter)
        code_icon.mousePressEvent = lambda e: self.launch_sub_program("MaterialCodeGenerator.py")
        
        # 待开发功能图标和标签
        coming_soon_icon = QLabel()
        coming_soon_icon.setPixmap(QPixmap("./icon/icon256x256.png").scaled(30, 30, Qt.KeepAspectRatio, Qt.SmoothTransformation))
        function_layout.addWidget(coming_soon_icon, 0, 2, alignment=Qt.AlignCenter)
        coming_soon_label = QLabel("待开发功能...")
        function_layout.addWidget(coming_soon_label, 1, 2, alignment=Qt.AlignCenter)
        #coming_soon_icon.mousePressEvent = self.show_coming_soon_dialog
        coming_soon_label.mousePressEvent = self.show_coming_soon_dialog
        
        function_combo_box.setLayout(function_layout)
        grid_layout.addWidget(function_combo_box, 1, 0)

    def show_about_dialog(self):
        about_dialog = AboutDialog()
        about_dialog.exec_()

    def show_theme_dialog(self):
        theme_dialog = ThemeDialog(self)
        theme_dialog.exec()

    def launch_sub_program(self, script_name):
        # 这里可以添加启动子程序的代码
        print(f"Launching {script_name}")
        # 例如，可以使用 subprocess.Popen 来启动新的 Python 脚本
        # import subprocess
        # subprocess.Popen(["python", script_name])

    def show_coming_soon_dialog(self, event):
        QMessageBox.information(self, "提示", "<blink>更多功能正在开发中，敬请期待！</blink>", QMessageBox.Ok)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())